/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2019/1/19
 * Time: 23:41
 */
$(function () {

    $('.input-group.date').datepicker({
        todayBtn: false,
        todayHighlight: true,
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true,
        format: "yyyy-mm-dd",
        language: "cn",
    });

    sendAjax('/v1/department/file.json', 'get', {}, function (json) {
        $('.department-name').text(json.body.name);
        var members = '';
        $.each(json.body.members, function (n, v) {
            members += v.realname + '，';
        });
        if (members == '') {
            members = '';
        } else {
            members = members.slice(0, -1);
            if (json.body.members.length >= 5) {
                members += '...<a href="#" data-toggle="modal" data-id="1" data-target="#normal-dialog">全部</a>';
            }
        }
        $('.department-members').html(members);
        $('.department-found-time').text(json.body.found_at);
        $('.department-desc').text(json.body.desc);
        $('.department-score').text(json.body.score);
        var star = '';
        var count = 0;
        if (json.body.score >= 90) {
            count = 5;
        } else if (json.body.score >= 60) {
            count = 3;
        } else {
            count = 0;
        }
        for (var i = 0; i < count; i ++) {
            star += '<i class="fa fa-star"></i>';
        }
        $('.department-star').html(star);
        $('.department-summary-text').val(json.body.summary);
    });

    $('#normal-dialog').on('show.bs.modal', function (event) {
        var id = $(event.relatedTarget).data('id');
        if (id == 1) {
            $(this).find('.modal-title').text('党支部成员');
            var modal_body = $(this).find('.modal-body');
            sendAjax('/v1/department/simple/users.json', 'get', {}, function(json){
                var data = json.body;

                if (data.length > 0) {
                    var html = '<div class="form-horizontal"><div class="form-group"><label class="col-sm-3 control-label">所有成员：</label><div class="col-sm-9">';
                    $.each(data, function(n, v){
                        html += '<span class="real-name" style="padding-right: 20px">' + v.realname + '</span>';
                    });
                    html += '</div></div></div>';
                    modal_body.html(html);
                }
            });
        } else {
            $('#normal-dialog .modal-header .modal-title').text('查看自我评价');
            $('#normal-dialog .modal-body').html('<div>'+$('.department-summary-text').val()+'</div>');
        }
        $(this).find('.modal-footer .dialog-btn-save').addClass('hidden');
    }).on('hide.bs.modal', function () {
        $(this).find('.modal-title').text('普通弹窗');
        $(this).find('.modal-body').html('');
        $(this).find('.modal-footer .dialog-btn-save').removeClass('hidden');
    });

    $('#edit-file').on('show.bs.modal', function () {
        $(this).find('.department-real-name').val($('.department-name').text());
        $(this).find('.department-found-at').val($('.department-found-time').text());
        $(this).find('.department-info').val($('.department-desc').text());
    }).on('hide.bs.modal', function () {
        $(this).find('.department-info').val('');
    });

    $('#edit-file .dialog-btn-save').on('click', function () {
        var params = {
            name:$('.department-real-name').val(),
            found_at:$('.department-found-at').val(),
            desc:$('.department-info').val(),
            summary: $('.summary').val()
        };
        if (checkForm(params)) {
            saveData('/v1/department/save/file', {params: params}, '', '更新成功');
        }
    });

    var url = '/v1/department/advice.json';
    var data = {
        year : 0,
        page : 1,
        page_size : 10
    };
    getDatas(url, data);

    $('.select-year').on('change', function () {
        data.year = $(this).val();
        data.page = 1;
        getDatas(url, data);

    });

    $(document).on('click', '.page-list a', function(){
        data.year = $('.select-year').val();
        data.page = $(this).attr('data-id');
        getDatas(url, data);
    });


});



function getDatas(url, data) {
    sendAjax(url, 'get', data, function (json) {
        var html = '';
        var page_html = '';
        if (json.rows.length > 0) {
            $.each(json.rows, function (n, v) {
                html += '<div class="row"><div class="col-xs-12 data-detail">';
                html += '<span>党委：</span><span class="department-name"></span>';
                html += '<span>&nbsp;时间：</span><span class="create-time">'+v.created_at+'</span></div>';
                html += '<div class="col-xs-12"><strong>意见内容：</strong></div>';
                html += '<div class="col-xs-12"><p class="advice-content">'+v.content+'</p></div></div>';
            });
            page_html = pageList(data.page, data.page_size, json.total, json.pages, json.rows.length);
        }
        $('.data-list').html(html);
        $('.page-list').html(page_html);
    });
}

function checkForm(data) {
    if (data.name == '') {
        alert('党支部名称不能为空');
        return false;
    }
    if (data.found_at == '') {
        alert('成立时间不能为空');
        return false;
    }
    return true;
}